<template>
  <div class="execution">
    <basic-container>
      <el-tabs v-model="activeName" @tab-click="handleClickTab">
        <el-tab-pane label="App轮播图" name="first"></el-tab-pane>
        <el-tab-pane label="小程序轮播图" name="second"></el-tab-pane>
        <el-tab-pane label="店铺轮播图" name="third"></el-tab-pane>

        <el-card
          class="filter-container"
          shadow="never"
          v-if="activeName !== 'second'">
          <div style="margin-top: 10px">
            <el-form :inline="true" size="small" label-width="140px">
              <el-form-item label="所属平台" v-if="activeName === 'first'">
                <el-select
                  v-model="listQuery.keyword"
                  clearable
                  placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="店铺：" v-if="activeName === 'third'">
                <el-input size="small" v-model="listQuery.keyword"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button @click="handleSecrch" type="primary" size="small"
                  >查询结果</el-button
                >
                <el-button @click="handleResetSearch" size="small">
                  重置
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>

        <el-card
          style="margin-top: 10px"
          shadow="never"
          v-if="activeName !== 'third'"
        >
          <el-button
            class="btn-add"
            icon="el-icon-plus"
            type="primary"
            @click="handleAdd()"
            size="mini"
          >
            添加
          </el-button>
        </el-card>
        <div style="margin-top: 10px">
          <el-table
            :data="list"
            border
            :header-cell-style="{ color: '#4D4D4D', background: '#fafafa' }"
          >
            <el-table-column label="轮播图图片" align="center">
              <template slot-scope="scope">
                <img
                  :src="scope.row.bannerImg"
                  style="width: 100px; height: 50px"
                />
              </template>
            </el-table-column>
            <el-table-column
              v-if="activeName === 'first'"
              label="跳转类型"
              align="center"
            >
              <template slot-scope="scope">{{
                scope.row.jumpType | jumpTypeFilters
              }}</template>
            </el-table-column>
            <el-table-column
              v-if="activeName === 'second'"
              label="跳转类型"
              align="center"
            >
              <template slot-scope="scope">{{
                scope.row.jumpType | TypeFilters
              }}</template>
            </el-table-column>
            <el-table-column align="center" prop="jumpUrl"></el-table-column>
            <el-table-column
              label="所属APP"
              align="center"
              prop="jumpApp"
            ></el-table-column>
            <el-table-column
              label="排序"
              align="center"
              prop="sort"
            ></el-table-column>
            <el-table-column
              label="操作人"
              align="center"
              prop="editName"
            ></el-table-column>
            <el-table-column
              label="操作时间"
              align="center"
              prop="updateTime"
            ></el-table-column>
            <el-table-column label="操作" align="center" v-if="activeName !== 'third'">
              <template slot-scope="scope">
                <el-button
                  size="small"
                  @click="handleExamine(scope.$index, scope.row)"
                  type="text"
                  icon="el-icon-edit"
                  >编辑</el-button
                >
                <el-button
                  size="small"
                  @click="handleDelete(scope.$index, scope.row)"
                  type="text"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination-container">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            layout="total, sizes,prev, pager, next,jumper"
            :page-size="listQuery.size"
            :page-sizes="[5, 10, 15]"
            :current-page.sync="listQuery.current"
            :total="total"
          >
          </el-pagination>
        </div>
      </el-tabs>

      <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        :before-close="close"
        width="40%"
      >
        <el-form
          :model="appAddForm"
          :rules="rules"
          ref="appAddForm"
          label-width="150px"
        >
          <el-form-item label="所属平台" v-if="activeName == 'first'">
            <el-select
              v-model="appAddForm.appId"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="图片：" prop="bannerImg">
            <single-upload v-model="appAddForm.bannerImg"></single-upload>
          </el-form-item>
          <el-form-item label="排序号：" prop="sort">
            <el-input v-model="appAddForm.sort"></el-input>
          </el-form-item>
          <el-form-item label="是否显示:" prop="isShow">
            <el-radio-group v-model="appAddForm.isShow">
              <el-radio :label="1">显示</el-radio>
              <el-radio :label="2">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否跳转:" prop="jumpType">
            <el-radio-group v-model="appAddForm.jumpType">
              <el-radio :label="1">不跳转</el-radio>
              <el-radio :label="2">商品</el-radio>
              <el-radio :label="3" v-if="activeName == 'first'">APP</el-radio>
              <el-radio :label="4">h5</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item
            label="链接："
            v-if="appAddForm.jumpType === 2 || appAddForm.jumpType === 4"
          >
            <el-input v-model="appAddForm.jumpUrl"></el-input>
          </el-form-item>
          <el-form-item label="App" v-if="appAddForm.jumpType == 3">
            <el-select
              v-model="appAddForm.jumpUrl"
              clearable
              placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('appAddForm')"
              >提交</el-button
            >
            <el-button type="info" @click="close()">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </basic-container>
  </div>
</template>
<script>
import "@/styles/index.scss";
import {
  appBannerList,
  htmlBannerList,
  storeBannerList,
  appAddBanner,
  appUpdateBanner,
  htmlAddBanner,
  htmlUpdateBanner,
  appDeleteBanner,
  htmlDeleteBanner
} from "@/api/home/banner";
import SingleUpload from "@/components/Upload/singleUpload";
import { platformAll } from "@/api/rms/rmsstore";

const defaultAppAddForm = {
  bannerImg: null,
  sort: null,
  isShow: 1,
  jumpType: 1,
  jumpUrl: null,
  appId: null,
};

export default {
  components: { SingleUpload },
  data() {
    return {
      title: "添加",
      activeName: "first",
      listQuery: {
        current: 1,
        size: 10,
        keyword: null,
      },
      list: [],
      total: 0,
      options: [],
      dialogVisible: false,
      rules: {
        bannerImg: [{ required: true, message: "请上传图片", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序号", trigger: "blur" }],
      },
      appAddForm: Object.assign({}, defaultAppAddForm),
    };
  },
  created() {
    this.getAppBanner();
    this.getPlatformAll();
  },
  filters: {
    jumpTypeFilters(value) {
      if (value === 1) {
        return "不跳转";
      } else if (value === 2) {
        return "跳转商品";
      } else if (value === 3) {
        return "跳转APP";
      } else if (value === 4) {
        return "跳转H5";
      }
    },
    TypeFilters(value) {
      if (value === 1) {
        return "不跳转";
      } else if (value === 2) {
        return "跳转商品";
      } else if (value === 4) {
        return "跳转小程序";
      }
    },
  },
  methods: {
    getPlatformAll() {
      platformAll().then(res => {
        this.options = res.data.data;
      })
    },
    handleClickTab(tab, event) {
      this.activeName = tab.name;
      if (this.activeName === "first") {
        this.listQuery.keyword = null;
        this.getAppBanner();
      } else if (this.activeName === "second") {
        this.listQuery.keyword = null;
        this.getHtmlBanner();
      } else if (this.activeName === "third") {
        this.listQuery.keyword = null;
        this.getStoreBannerList();
      }
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      if (this.activeName === "first") {
        this.getAppBanner();
      } else if (this.activeName === "second") {
        this.getHtmlBanner();
      } else if (this.activeName === "third") {
        this.getStoreBannerList();
      }
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      if (this.activeName === "first") {
        this.getAppBanner();
      } else if (this.activeName === "second") {
        this.getHtmlBanner();
      } else if (this.activeName === "third") {
        this.getStoreBannerList();
      }
    },
    getAppBanner() {
      appBannerList(this.listQuery).then((res) => {
        this.list = res.data.data.records;
        this.total = res.data.data.total;
      });
    },
    getHtmlBanner() {
      htmlBannerList(this.listQuery).then((res) => {
        this.list = res.data.data.records;
        this.total = res.data.data.total;
      });
    },
    getStoreBannerList() {
      storeBannerList(this.listQuery).then((res) => {
        this.list = res.data.data.records;
        this.total = res.data.data.total;
      });
    },
    handleSecrch() {
      if (this.activeName === "first") {
        this.getAppBanner();
      } else if (this.activeName === "third") {
        this.getStoreBannerList();
      }
    },
    handleResetSearch() {
      if (this.activeName === "first") {
        this.listQuery.keyword = null;
        this.getAppBanner();
      } else if (this.activeName === "third") {
        this.listQuery.keyword = null;
        this.getStoreBannerList();
      }
    },

    handleAdd() {
      this.dialogVisible = true;
    },
    handleExamine(index, row) {
      this.title = "编辑";
      this.appAddForm = Object.assign({}, row);
      this.dialogVisible = true;
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.activeName === "first") {
            this.handelAddApp();
          } else if (this.activeName === "second") {
            this.handelAddHtml();
          }
        }
      });
    },
    handelAddApp() {
      // if (!this.appAddForm.appName) {
      //   this.$message.warning("请选择所属平台！");
      // }

      if (this.appAddForm.jumpType !== 1) {
        if (!this.appAddForm.jumpUrl) {
          this.$message.warning("请填写链接地址！");
          return;
        }
      }
      if (this.title == "编辑") {
        appUpdateBanner(this.appAddForm).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              message: res.data.msg,
              type: "success",
              duration: 1000,
            });
            this.getAppBanner();
            this.close();
          } else {
            this.$message({
              message: res.data.msg,
              type: "warning",
              duration: 1000,
            });
          }
        });
      } else {
        appAddBanner(this.appAddForm).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              message: res.data.msg,
              type: "success",
              duration: 1000,
            });
            this.getAppBanner();
            this.close();
          } else {
            this.$message({
              message: res.data.msg,
              type: "warning",
              duration: 1000,
            });
          }
        });
      }
    },
    handelAddHtml() {
      if (this.appAddForm.jumpType !== 1) {
        if (!this.appAddForm.jumpUrl) {
          this.$message.warning("请填写链接地址！");
          return;
        }
      }
      if (this.title == "编辑") {
        htmlUpdateBanner(this.appAddForm).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              message: res.data.msg,
              type: "success",
              duration: 1000,
            });
            this.getHtmlBanner();
            this.close();
          } else {
            this.$message({
              message: res.data.msg,
              type: "warning",
              duration: 1000,
            });
          }
        });
      } else {
        htmlAddBanner(this.appAddForm).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              message: res.data.msg,
              type: "success",
              duration: 1000,
            });
            this.getHtmlBanner();
            this.close();
          } else {
            this.$message({
              message: res.data.msg,
              type: "warning",
              duration: 1000,
            });
          }
        });
      }
    },
    close() {
      this.dialogVisible = false;
      this.appAddForm = Object.assign({}, defaultAppAddForm);
    },
    handleDelete(index,row) {
      if (this.activeName == "first") {
        this.hadleDeleteApp(row)
      } else if (this.activeName == "second") {
        this.hadleDeleteHtml(row)
      }
    },
    hadleDeleteApp(row) {
      let that = this;
      that.$confirm("是否确认删除ID为" + row.id, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        appDeleteBanner(row.id).then(res => {
          if (res.data.code === 200) {
            that.$message({
              message: res.data.msg,
              type: "success",
              duration: 1000,
            });
            that.getAppBanner();
          } else {
            that.$message({
              message: res.data.msg,
              type: 'warning',
              duration: 1000
            });
          }
        })
      });
    },
    hadleDeleteHtml(row) {
      let that = this;
      that.$confirm("是否确认删除ID为" + row.id, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        htmlDeleteBanner(row.id).then(res => {
          if (res.data.code === 200) {
            that.$message({
              message: '删除成功',
              type: 'success',
              duration: 1000
            });
            that.getHtmlBanner();
          } else {
            that.$message({
              message: res.data.msg,
              type: 'warning',
              duration: 1000
            });
          }
        })
      });
    }
  },
};
</script>
